﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Ctrls;
using CommunitySystem.Views;
using System.Collections.ObjectModel;

namespace CommunitySystem.PageView
{
    public partial class KaoChaControl : AsyncCtrl, IReturnListenerc
    {
        private JinShengService.JinShengServiceClient _client = new JinShengService.JinShengServiceClient();
        private JinShengService.JinShen_ChooseCandidator_Main chooseMain = null;
        private JinShengService.JinShen_MainInfo _mainInfo = null;
        private ObservableCollection<JinShengService.JinShen_KaoCha_Detail> details = new ObservableCollection<JinShengService.JinShen_KaoCha_Detail>();

        private bool CanEditInfo = true;
        private bool CanSubmit = true;
        private bool CanConfirm = false;

        public KaoChaControl(JinShengService.JinShen_MainInfo mainInfo)
        {
            InitializeComponent();
            _mainInfo = mainInfo;
            details.Clear();

            _client.GetOrgForHRCompleted += (sender, e) =>
            {
                if (JinShenConst.Administrator.Contains(e.Result))
                {                   
                    CanEditInfo = false;
                    CanSubmit = false;
                    CanConfirm = true;
                }
                if (_mainInfo.Status == JinShenConst.STATUS_Promot_Agree)
                {
                    CanSubmit = false;
                    CanEditInfo = false;
                }
                
                btnSubmit.IsEnabled = CanSubmit;

            };
            _client.GetOrgForHRAsync(EHR.Common.Session.LoginUser.LoginName);


            _client.GetJinShen_ChooseCandidator_MainCompleted += (sender, e) =>
            {
                chooseMain = e.Result.LastOrDefault();
                Init();
            };
            _client.GetJinShen_ChooseCandidator_MainAsync("MainID=" + mainInfo.ID);
            _client.SaveJinShen_KaoCha_DetailCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    SilverlightMessageBoxLibrary.Message.InfoMessage("上报成功");
                    imgBtn2.IsEnabled = true;
                }
            };

            _client.SaveJinShenMainInfoCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    _client.SaveJinShen_KaoCha_DetailAsync(details);
                }
            };
        }

        

        public void BeginReturn()
        {
            return;
        }
        
        private void Init()
        {
            if (_mainInfo.Status == JinShenConst.STATUS_Candidate_Inspect)//新增
            {
                foreach (var d in chooseMain.Details)
                {
                    JinShengService.JinShen_KaoCha_Detail detail = new JinShengService.JinShen_KaoCha_Detail() 
                    {
                        MainID = _mainInfo.ID, 
                        CreateDate = DateTime.Now, 
                        CreateUser = EHR.Common.Session.LoginUser.LoginName, 
                        Status = JinShenConst.STATUS_Candidate_Inspect,
                        EmpID = d.EmpID,
                        EmpName = d.Name,
                    };
                    details.Add(detail);                    
                }
                LoadUI();
            }
            else if (_mainInfo.Status == JinShenConst.STATUS_Party_Meeting
                || _mainInfo.Status == JinShenConst.STATUS_Candidate_Show
                || _mainInfo.Status == JinShenConst.STATUS_Promot_Apply//编辑
                || _mainInfo.Status == JinShenConst.STATUS_Promot_Agree)//查看
            {
                _client.GetJinShen_KaoCha_DetailCompleted += (sender, e) =>
                {
                    details = e.Result;
                    imgBtn2.IsEnabled = true;
                    LoadUI();
                };
                _client.GetJinShen_KaoCha_DetailAsync("MainID=" + _mainInfo.ID);
            }
            
        }

        private void LoadUI()
        {
            spDetails.Children.Clear();
            foreach (var d in details)
            {
                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(3) };
                TextBlock tb = new TextBlock() { Text = d.EmpName, Width=80 };
                TextBox tBox = new TextBox() { Height = 60, Width = 400, Margin = new Thickness(6, 0, 0, 0) };
                tBox.IsReadOnly = !CanEditInfo;
                tBox.SetBinding(TextBox.TextProperty, new System.Windows.Data.Binding("DSC") { Mode = System.Windows.Data.BindingMode.TwoWay });
                tBox.DataContext = d;
                sp.Children.Add(tb);
                sp.Children.Add(tBox);
                spDetails.Children.Add(sp);
            }
        }

        /// <summary>
        /// 考察情况上报
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            if (Validate())
            {
                _mainInfo.Status = JinShenConst.STATUS_Candidate_Inspect;
                _client.SaveJinShenMainInfoAsync(_mainInfo);
            }
        }

        private bool Validate()
        {
            var detail = details.Where(c => string.IsNullOrEmpty(c.DSC)).FirstOrDefault();
            if (detail != null)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("请填写" + detail.EmpName + "考察信息");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 下一部
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageButton_Click_1(object sender, RoutedEventArgs e)
        {
            //ColumnsInSelectControl view = new ColumnsInSelectControl(ele) { Nav = flip.pageFrame as NavMoveSwitch };
            //Nav.ShowNextPage(item.Name, view, flip.newPage as IReturnListenerc);
            JinShen_DangGongWeiControl control = new JinShen_DangGongWeiControl(_mainInfo) { Nav = this.Nav };
            Nav.ShowNextPage("党(工)委(组)考察", control as AsyncCtrl);
        }
    }
}
